package be.redo.controller;

import be.redo.model.entities.Residence;
import be.redo.service.RegionService;
import be.redo.service.ResidenceService;
import be.redo.service.StayService;
import java.io.UnsupportedEncodingException;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.util.UriUtils;
import org.springframework.web.util.WebUtils;

@RequestMapping("/residences")
@Controller
public class ResidenceController {

	@Autowired
	ResidenceService residenceService;

	@Autowired
	RegionService regionService;

	@Autowired
	StayService stayService;

	@RequestMapping(method = RequestMethod.POST, produces = "text/html")
	public String create(@Valid Residence residence,
			BindingResult bindingResult, Model uiModel,
			HttpServletRequest httpServletRequest) {
		if (bindingResult.hasErrors()) {
			populateEditForm(uiModel, residence);
			return "residences/create";
		}
		uiModel.asMap().clear();
		residenceService.create(residence);
		return "redirect:/residences/"
				+ encodeUrlPathSegment(residence.getResidenceId().toString(),
						httpServletRequest);
	}

	@RequestMapping(params = "form", produces = "text/html")
	public String createForm(Model uiModel) {
		populateEditForm(uiModel, new Residence());
		return "residences/create";
	}

	@RequestMapping(value = "/{ResidenceId}", produces = "text/html")
	public String show(@PathVariable("ResidenceId") Long ResidenceId,
			Model uiModel) {
		uiModel.addAttribute("residence", residenceService.read(ResidenceId));
		uiModel.addAttribute("itemId", ResidenceId);
		return "residences/show";
	}

	/*
	 * @RequestMapping(produces = "text/html") public String
	 * list(@RequestParam(value = "page", required = false) Integer page,
	 * @RequestParam(value = "size", required = false) Integer size, Model
	 * uiModel) { if (page != null || size != null) { int sizeNo = size == null
	 * ? 10 : size.intValue(); final int firstResult = page == null ? 0 :
	 * (page.intValue() - 1) * sizeNo; uiModel.addAttribute("residences",
	 * residenceService.findResidenceEntries(firstResult, sizeNo)); float
	 * nrOfPages = (float) residenceService.countAllResidences() / sizeNo;
	 * uiModel.addAttribute("maxPages", (int) ((nrOfPages > (int) nrOfPages ||
	 * nrOfPages == 0.0) ? nrOfPages + 1 : nrOfPages)); } else {
	 * uiModel.addAttribute("residences", residenceService.findAllResidences());
	 * } return "residences/list"; }
	 */
	@RequestMapping(method = RequestMethod.PUT, produces = "text/html")
	public String update(@Valid Residence residence,
			BindingResult bindingResult, Model uiModel,
			HttpServletRequest httpServletRequest) {
		if (bindingResult.hasErrors()) {
			populateEditForm(uiModel, residence);
			return "residences/update";
		}
		uiModel.asMap().clear();
		residenceService.update(residence);
		return "redirect:/residences/"
				+ encodeUrlPathSegment(residence.getResidenceId().toString(),
						httpServletRequest);
	}

	@RequestMapping(value = "/{ResidenceId}", params = "form", produces = "text/html")
	public String updateForm(@PathVariable("ResidenceId") Long ResidenceId,
			Model uiModel) {
		populateEditForm(uiModel, residenceService.read(ResidenceId));
		return "residences/update";
	}

	@RequestMapping(value = "/{ResidenceId}", method = RequestMethod.DELETE, produces = "text/html")
	public String delete(@PathVariable("ResidenceId") Long ResidenceId,
			@RequestParam(value = "page", required = false) Integer page,
			@RequestParam(value = "size", required = false) Integer size,
			Model uiModel) {
		Residence residence = residenceService.read(ResidenceId);
		residenceService.delete(residence.getResidenceId());
		uiModel.asMap().clear();
		uiModel.addAttribute("page", (page == null) ? "1" : page.toString());
		uiModel.addAttribute("size", (size == null) ? "10" : size.toString());
		return "redirect:/residences";
	}

	void populateEditForm(Model uiModel, Residence residence) {
		uiModel.addAttribute("residence", residence);
		uiModel.addAttribute("regions", regionService.findAll());
		uiModel.addAttribute("stays", stayService.findAll());
	}

	String encodeUrlPathSegment(String pathSegment,
			HttpServletRequest httpServletRequest) {
		String enc = httpServletRequest.getCharacterEncoding();
		if (enc == null) {
			enc = WebUtils.DEFAULT_CHARACTER_ENCODING;
		}
		try {
			pathSegment = UriUtils.encodePathSegment(pathSegment, enc);
		} catch (UnsupportedEncodingException uee) {
		}
		return pathSegment;
	}
}
